home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / dbu / rdwrbatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-23  |  3.4 KB  |  197 lines

  1. # include    <ingres.h>
  2. # include    <aux.h>
  3. # include    <symbol.h>
  4. # include    <access.h>
  5. # include    <batch.h>
  6. # include    <sccs.h>
  7.  
  8. SCCSID(@(#)rdwrbatch.c    8.2    2/23/85)
  9.  
  10. /*
  11. **    GETBATCH - retrieve data from the batch file and place in given
  12. **            location
  13. **
  14. **
  15. **    Parameters:
  16. **        loc - address of place to put the data from the batch file
  17. **        count - amount of data to get
  18. **
  19. **    Return Codes:
  20. **            0
  21. **
  22. **    Side Effects:
  23. **        loc is filled with data from batch file
  24. **
  25. **    Trace Flags:
  26. **        Z42.15
  27. **
  28. **    Called by:
  29. **        modupdate
  30. **        secupdate
  31. **        update
  32. **        btreeupdate
  33. **
  34. */
  35. getbatch(loc, count)
  36. char    *loc;
  37. int    count;
  38. {
  39.     register char    *c;
  40.     register int    cnt, size;
  41.     int        i;
  42.  
  43.     cnt = count;
  44. #    ifdef xZTR1
  45.     if (tTf(42, 15))
  46.         printf("getbatch:%d (%d)\n", cnt, Batch_cnt);
  47. #    endif
  48.     c = loc;
  49.  
  50.     while (cnt)
  51.     {
  52.         /* see if there is anything in the buffer */
  53.         if (Batch_cnt == BATCHSIZE)
  54.             if ((i = readbatch()) < cnt)
  55.                 syserr("getbatch:can't read enough %d %d", i, cnt);
  56.         if (cnt <= BATCHSIZE - Batch_cnt)
  57.             size = cnt;
  58.         else
  59.             size = BATCHSIZE - Batch_cnt;
  60.         bmove(&Batchbuf.bbuf[Batch_cnt], c, size);
  61.         Batch_cnt += size;
  62.         cnt -= size;
  63.         c += size;
  64.         /* flush the buffer if full */
  65.         if (Batch_cnt == BATCHSIZE)
  66.             batchflush();    /* re-write buffer if necessary */
  67.     }
  68.     return (0);
  69. }
  70.  
  71. /*
  72. **    PUTBATCH - put data in the batch file
  73. **
  74. **
  75. **    Parameters:
  76. **        cp - location of data to put in batch file
  77. **        count - size of data
  78. **
  79. **    Return Codes:
  80. **            0
  81. **
  82. **    Trace Flags:
  83. **        Z42.2
  84. **
  85. **    Side Effects:
  86. **        data is placed in batch file
  87. **
  88. **    Called by:
  89. **        update
  90. **
  91. */
  92.  
  93. putbatch(cp, count)
  94. char    *cp;
  95. int    count;
  96. {
  97.     register char    *c;
  98.     register int    size, cnt;
  99.     int        i;
  100.  
  101.     cnt = count;
  102.     c = cp;
  103. #    ifdef xZTR1
  104.     if (tTf(42, 2))
  105.         printf("putbatch:%d\n", cnt);
  106. #    endif
  107.  
  108.     while (cnt)
  109.     {
  110.         Batch_dirty = TRUE;    /* mark this buffer as dirty */
  111.         if (cnt + Batch_cnt > BATCHSIZE)
  112.             size = BATCHSIZE - Batch_cnt;
  113.         else
  114.             size = cnt;
  115.         bmove(c, &Batchbuf.bbuf[Batch_cnt], size);
  116.         c += size;
  117.         Batch_cnt += size;
  118.         cnt -= size;
  119.         if (Batch_cnt == BATCHSIZE)
  120.         {
  121.             batchflush();
  122.             /* is there is more to write, must read ahead first */
  123.             if (cnt)
  124.                 if ((i = readbatch()) < cnt)
  125.                     syserr("putbatch:rd too small %d", i);
  126.         }
  127.     }
  128. }
  129.  
  130. /*
  131. **    READBATCH - read the batch file
  132. **
  133. **    Return Codes:
  134. **        returns number of bytes read
  135. **
  136. **    Trace Flags:
  137. **        Z42.10
  138. **
  139. **    Called by:
  140. **        update()
  141. **
  142. */
  143.  
  144. readbatch()
  145. {
  146.  
  147.     if ((Batch_lread = read(Batch_fp, &Batchbuf, BATCHSIZE+IDSIZE)) < 0)
  148.         syserr("readbatch:can't read %d %d", Batch_lread, Batch_fp);
  149.     Batch_cnt = 0;
  150. #    ifdef xZTR1
  151.     if (tTf(42, 10))
  152.         printf("read %d bytes from batch\n", Batch_lread);
  153. #    endif
  154.     /* check file-id */
  155.     if (!sequal(Fileset, Batchbuf.file_id))
  156.         syserr("readbatch:bad id '%s' '%.20s' %d", Fileset, Batchbuf.file_id, Batch_lread);
  157.     return (Batch_lread);
  158. }
  159.  
  160. /*
  161. **    BATCHFLUSH - flush the batch file
  162. **
  163. **    Return Codes:
  164. **            0
  165. **
  166. **    Trace Flags:
  167. **        Z42.4, Z42.5
  168. **
  169. **    Side Effects:
  170. **        Batch_dirty gets FALSE
  171. **
  172. **    Called by:
  173. **        update
  174. **        getbatch
  175. */
  176.  
  177. batchflush()
  178. {
  179.     register int    i;
  180.     if (Batch_cnt && Batch_dirty)
  181.     {
  182. #        ifdef xZTR1
  183.         if (tTf(42, 5))
  184.             printf("flush:backing up %d\n", Batch_lread);
  185. #        endif
  186.         if ((i = lseek(Batch_fp, (long) -Batch_lread, 1)) < 0)
  187.             syserr("batchflush:can't seek %d", Batch_lread);
  188. #        ifdef xZTR1
  189.         if (tTf(42, 4))
  190.             printf("flushing %d\n", Batch_cnt + IDSIZE);
  191. #        endif
  192.         if ((i = write(Batch_fp, &Batchbuf, Batch_cnt + IDSIZE)) != Batch_cnt + IDSIZE)
  193.             syserr("batchflush:can't write %d", i);
  194.         Batch_dirty = FALSE;
  195.     }
  196. }
  197.